//--------------------------------------------start SCORM---------------------------------------------------
//define a SCORM object to interface with LMS
var oScorm = pipwerks.SCORM;  
var name = sessionStorage.getItem( "learnername" );

// This function starts the course.  This function should be called first to ensure that
// all session variables are created
function loadOLM()
{
	// check to see if cookies is enabled, otherwise, this won't work
	if( typeof sessionStorage == "undefined" || sessionStorage.disabled )
	{
		alert( "You must enable cookies for this course to work properly." );
	}
	else
	{
		// if it is enabled, then check to see if this course was started
		// if it was not able to retrieve, the key "course_started", then
		// then this is the first time we are running the course
        if( !sessionStorage.getItem( "course_started" ) )
        {
        	// clear the session storage
            sessionStorage.clear();
            // set the "key" by giving it a value.  all values are strings!
            // course_started indicated that this OLM has started
    		sessionStorage.setItem( "course_started", "1" );
    			
			// set the values you want to persist throughout the website here
        
        
        }
	}
	
	// initializeSCORM after the OLM environment has been set up
	initializeSCORM();
}

// This function initializes the course by connecting the OLM to the LMS 
// The SCORM connection is done once at the beginning of the course
// Another function, reportScores(), will connect again when the 
// user is done with the online learning module
function initializeSCORM()
{
	var lmsConnected = oScorm.init();
	
	// we only want to run initializeSCORM once, so use course_started
	// to keep track of how many times we have initialized 
	var getStarted = sessionStorage.getItem( "course_started" );
	
	// only do the following if we are connected to the LMS and
	// if course_started has a value of 1
	if( lmsConnected && getStarted == "1" )
	{
        // always set the status to incomplete
		oScorm.set( "cmi.success_status", "unknown" );
		oScorm.set( "cmi.lesson_status", "incomplete" );
		oScorm.set( "cmi.completion_status", "incomplete" );
		
		// change course_started to another value so that this initializeSCORM
		// does not run the initialization code again; otherwise, this OLM
		// will be incomplete if the user goes back to the first page even after
		// passing the exam 
		sessionStorage.setItem( "course_started", "2" );
    		
		// retrieve the LMS values here and integrate them into your course
		var name = oScorm.get( "cmi.learner_name" );
		sessionStorage.setItem( "learnername", name );
	}
}

function userName()
{
	document.getElementById( "learnername" ).innerHTML = name;
}

// This function reports the score from the assessment to the LMS
// This should only be called when the user submits the answers to the quiz
function reportScores( trueScore )
{	
	alert( "REPORTED" );
	// these are some test values
	oScorm.set("cmi.score.raw", trueScore );
	oScorm.set("cmi.score.min", 0 );
	oScorm.set("cmi.score.max", 100 );
	oScorm.set("cmi.score.scaled", trueScore/100 );
	
	// Set the values and status here
	// for now, just set it to passed and completed
	oScorm.set( "cmi.success_status", "passed" );
	oScorm.set( "cmi.completion_status", "completed" );
	oScorm.set( "cmi.lesson_status", "passed" );

	oScorm.save();
	//alert( "finished reporting");
	//alert( "true score value is " + trueScore );
}

// This function is called when the window is closed.  It saves and quits the course.
function finishOLM()
{
	oScorm.save();
	oScorm.quit();
}

//------------------------------------------start sessionStorage----------------------------------------
//sessionStorage.setItem( "somekey", "somevalue");
//var value = sessionStorage.getItem( "somekey" );

function startSession() //call on the very first time to the website
{
	var fw = sessionStorage.getItem( "firstWelcome" );
	
	if( fw == null ) //null means it's not there, it does not exist
	{
		sessionStorage.setItem( "firstWelcome", "1" );
		sessionStorage.setItem( "visitSun", "0" );
		sessionStorage.setItem( "visitTerresterial", "0" );
		sessionStorage.setItem( "visitMercury", "0" );
		sessionStorage.setItem( "visitVenus", "0" );
		sessionStorage.setItem( "visitEarth", "0" );
		sessionStorage.setItem( "visitMars", "0" );
		sessionStorage.setItem( "visitTerresterialQuiz", "0" );
		sessionStorage.setItem( "passTerresterialQuiz", "0" );
		sessionStorage.setItem( "visitMoon", "0" );
		sessionStorage.setItem( "visitAsteroid", "0" );
		sessionStorage.setItem( "visitGaseous", "0" );
		sessionStorage.setItem( "visitJupiter", "0" );
		sessionStorage.setItem( "visitSaturn", "0" );
		sessionStorage.setItem( "visitUranus", "0" );
		sessionStorage.setItem( "visitNeptune", "0" );
		sessionStorage.setItem( "visitGaseousQuiz", "0" );
		sessionStorage.setItem( "passGaseousQuiz", "0" );
	}
}
function visitSun()
{
	sessionStorage.setItem( "visitSun", "1" );
	showQuiz();
}
function visitTerresterial()
{
	sessionStorage.setItem ( "visitTerresterial", "1" );
	showQuiz();
}
function visitMercury()
{
	sessionStorage.setItem ( "visitMercury", "1" );
	showQuiz();
}
function visitVenus()
{
	sessionStorage.setItem ( "visitVenus", "1" );
	showQuiz();
}
function visitEarth()
{
	sessionStorage.setItem ( "visitEarth", "1" );
	showQuiz();
}
function visitMars()
{
	sessionStorage.setItem ( "visitMars", "1" );
	showQuiz();
}
function visitTerresterialQuiz()
{
	sessionStorage.setItem ( "visitTerresterialQuiz", "1" )
	showQuiz();
}
function visitMoon()
{
	sessionStorage.setItem ( "visitMoon", "1" );
	showQuiz();
}
function visitAsteroid()
{
	sessionStorage.setItem ( "visitAsteroid", "1" );
	showQuiz();
}
function visitGaseous()
{
	sessionStorage.setItem ( "visitGaseous", "1" );
	showQuiz();
}
function visitJupiter()
{
	sessionStorage.setItem ( "visitJupiter", "1" );
	showQuiz();
}
function visitSaturn()
{
	sessionStorage.setItem ( "visitSaturn", "1" );
	showQuiz();
}
function visitUranus()
{
	sessionStorage.setItem ( "visitUranus", "1" );
	showQuiz();
}
function visitNeptune()
{
	sessionStorage.setItem ( "visitNeptune", "1" );
	showQuiz();
}
function visitGaseousQuiz()
{
	sessionStorage.setItem ( "visitGaseousQuiz", "1" );
	showQuiz();
}
function visitFinalQuiz()
{
	showQuiz();
}
function showCertificate()
{
	var fq = sessionStorage.getItem( "finalquiz" );
	if( fq >= 14 )
	{
		document.getElementById( "certificate" ).className = "show";
	}
}
function showQuiz()
{
	var fw = sessionStorage.getItem( "firstWelcome" );
	var vsun = sessionStorage.getItem( "visitSun" );
	var vter = sessionStorage.getItem( "visitTerresterial" );
	var vmer = sessionStorage.getItem( "visitMercury" );
	var vven = sessionStorage.getItem( "visitVenus" );
	var vear = sessionStorage.getItem( "visitEarth" );
	var vmar = sessionStorage.getItem( "visitMars" );
	var vterq = sessionStorage.getItem( "visitTerresterialQuiz" );
	var pterq = sessionStorage.getItem( "passTerresterialQuiz" );
	var vmoo = sessionStorage.getItem( "visitMoon" );
	var vast = sessionStorage.getItem( "visitAsteroid" );
	var vgas = sessionStorage.getItem( "visitGaseous" );
	var vjup = sessionStorage.getItem( "visitJupiter" );
	var vsat = sessionStorage.getItem( "visitSaturn" );
	var vura = sessionStorage.getItem( "visitUranus" );
	var vnep = sessionStorage.getItem( "visitNeptune" );
	var vgasq = sessionStorage.getItem( "visitGaseousQuiz" );	
	var pgasq = sessionStorage.getItem( "passGaseousQuiz" );
	
//terresterial quiz
	if( vter == "1" && vmer == "1" && vven == "1" && vear == "1" && vmar == "1" )
	{
		//show quiz link, pull id from html and change style.display
		document.getElementById( "terresterialquizlink" ).className = "show";
	}
//gaseousquiz
	if( vterq == "1" && vgas == "1" && vjup == "1" && vsat == "1" && vura == "1" && vnep == "1" )
	{
		//show quiz link, pull id from html and change style.display
		document.getElementById( "gaseousquizlink" ).className = "show";
	}
	
//finalquiz
	if( fw == "1" && vsun == "1" 
		&& vter == "1" && vmer == "1" && vven == "1" && vear == "1" && vmar == "1" && pterq == "1"
		&& vmoo == "1" && vast == "1"
		&& vgas == "1" && vjup == "1" && vsat == "1" && vura == "1" && vnep == "1" && pgasq == "1" )
	{
		//show quiz link, pull id from html and change style.display
		document.getElementById( "finalquizlink" ).className = "show";
	}
	showCertificate();
}